package com.huohuzhihui.web.controller.api;

import com.huohuzhihui.common.config.RuoYiConfig;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.utils.file.FileUploadUtils;
import com.huohuzhihui.common.utils.file.MimeTypeUtils;
import com.huohuzhihui.framework.config.ServerConfig;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.MerResource;
import com.huohuzhihui.merchant.service.IMerResourceService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 营销活动
 *
 * @author lizhihe
 * @date 2020-11-22
 */
@RestController
@RequestMapping("/api/video")
public class ApiVideoController extends BaseController {
    @Autowired
    private IMerResourceService merResourceService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ServerConfig serverConfig;


    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest request, Long resourceGroupId) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file, ArrayUtils.addAll(MimeTypeUtils.IMAGE_EXTENSION, MimeTypeUtils.MEDIA_EXTENSION));
            String url = serverConfig.getUrl() + fileName;

            //保存视频
            LoginUser loginUser = tokenService.getLoginUser(request);
            SysUser sysUser = loginUser.getUser();
            Long merchantId = sysUser.getDeptId();
            MerResource merResource = new MerResource();
            merResource.setMerchantId(merchantId);
            merResource.setGroupId(resourceGroupId);
            merResource.setResourceUrl(url);
            merResource.setCreateBy(sysUser.getUserName());
            this.merResourceService.insertMerResource(merResource);

            return AjaxResult.success("上传成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }





}
